package com.tgy.leetcode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * https://leetcode-cn.com/problems/flatten-nested-list-iterator/
 *
 */
 interface NestedInteger {

      // @return true if this NestedInteger holds a single integer, rather than a nested list.
      public boolean isInteger();

     // @return the single integer that this NestedInteger holds, if it holds a single integer
      // Return null if this NestedInteger holds a nested list
      public Integer getInteger();

     // @return the nested list that this NestedInteger holds, if it holds a nested list
      // Return null if this NestedInteger holds a single integer
      public List<NestedInteger> getList();
  }

  class NestedIterator implements Iterator<Integer> {


     private List<NestedInteger> allNestedInteger;
     private Iterator<NestedInteger> iterator;

    public NestedIterator(List<NestedInteger> nestedList) {

        allNestedInteger = new ArrayList<>();
        for (NestedInteger nestedInteger : nestedList) {
            dfs(nestedInteger);
        }

        iterator = allNestedInteger.iterator();
    }

    @Override
    public Integer next() {


        return iterator.next().getInteger();
    }

    @Override
    public boolean hasNext() {

        return iterator.hasNext();
    }

    private void dfs(NestedInteger nestedInteger) {

        if (nestedInteger.isInteger()) {

            allNestedInteger.add(nestedInteger);
            return;
        }
        for (NestedInteger integer : nestedInteger.getList()) {
            dfs(integer);
        }
    }

}

public class _341_扁平化嵌套列表迭代器 {


}
